package com.wuxiaoqin.server.controller;


import com.wuxiaoqin.server.entity.Address;
import com.wuxiaoqin.server.entity.Users;
import com.wuxiaoqin.server.service.IAddressService;
import com.wuxiaoqin.server.service.IUsersService;
import com.wuxiaoqin.uitls.respObj.RespBean;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2021-10-29
 */
@RestController
@ApiOperation("地址操作")
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private IAddressService addressService;

    @Autowired
    private IUsersService usersService;

    @ApiOperation(value = "获取用户收货地址")
    @GetMapping("/getAddress")
    public List<Address> addressModify() {
        Users currentUser = usersService.getCurrentUser();
        return addressService.getAddress(currentUser.getUId());

    }



    @ApiOperation(value = "修改用户地址")
    @PutMapping("/addressModify")
    public RespBean addressModify(@RequestParam("addressid") String addressid,
                                  @RequestParam("address") String address,
                                  @RequestParam("name") String name,
                                  @RequestParam("phone") String phone) {
        Users currentUser = usersService.getCurrentUser();
        RespBean respBean=addressService.addressModify(currentUser.getUId(),addressid,address,name,phone);
        return respBean;
    }

    @ApiOperation(value = "将地址设置成默认地址")
    @PutMapping("/addStateModify")
    public RespBean addStateModify(@RequestParam("addressid") Integer addresid) {
        Users currentUser = usersService.getCurrentUser();
        RespBean respBean=addressService.addStateModify(currentUser.getUId(),addresid);
        return respBean;
    }
    @ApiOperation(value = "添加地址")
    @GetMapping("/addAddress")
    public RespBean addAddress(@RequestParam("address") String address,
                               @RequestParam("name") String name,
                               @RequestParam("phone") String phone,
                               @RequestParam(value = "state",defaultValue = "1") Integer state ) {
        Users currentUser = usersService.getCurrentUser();
        RespBean respBean=addressService.addAddress(currentUser.getUId(),address,name,phone,state);
        return respBean;
    }
    @ApiOperation(value = "删除地址")
    @DeleteMapping("/deleteAddress")
    public RespBean deleteAddress(@RequestParam("addressId") Integer addressId) {

        RespBean respBean=addressService.deleteAddress(addressId);
        return respBean;
    }
}
